package com.flkin.modules.system.controller;

import com.flkin.common.base.Ret;
import com.flkin.modules.system.domain.User;
import com.flkin.modules.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yanxiang.huang 2020-12-07 22:38:02
 */
@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping
    @PreAuthorize("@sec.check('userRead')")
    public ResponseEntity<Object> getAll() {
        return Ret.ok(userService.getAll());
    }

    @GetMapping("/{id}")
    @PreAuthorize("@sec.check('userRead')")
    public ResponseEntity<Object> findById(@PathVariable Long id) {
        return Ret.ok(userService.findById(id));
    }

    @PostMapping
    @PreAuthorize("@sec.check('userCreate')")
    public ResponseEntity<Object> create(@RequestBody User resource) {
        return Ret.ok(userService.create(resource));
    }

    @PutMapping
    @PreAuthorize("@sec.check('userUpdate')")
    public ResponseEntity<Object> update(@RequestBody User resource) {
        userService.update(resource);
        return Ret.ok();
    }

    @DeleteMapping
    @PreAuthorize("@sec.check('userDelete')")
    public ResponseEntity<Object> delete(@RequestBody List<Long> ids) {
        userService.deleteAll(ids);
        return Ret.ok();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@sec.check('userDelete')")
    public ResponseEntity<Object> delete(@PathVariable Long id) {
        userService.deleteById(id);
        return Ret.ok();
    }

}
